﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace Radiation_client
{
    class Program
    {
        private static byte[] buffer;
        private static int buffer_size = 65565;

        static void Main(string[] args)
        {
            try
            {
                buffer = new byte[buffer_size];

                Console.WriteLine("IP du serveur : ");
                string ip = Console.ReadLine();
                Console.WriteLine("Port : ");
                string port = Console.ReadLine();


                TcpClient client = new TcpClient();
                client.Connect(ip, Convert.ToInt32(port));
                client.Client.BeginReceive(buffer, 0, buffer_size, SocketFlags.None, new AsyncCallback(dispMsg), client);

                Console.WriteLine("Write some text to send : ");
                while (true)
                {
                    string text = Console.ReadLine();
                    if (text.Length > 0 && text.StartsWith("["))
                        client.Client.Send(UnicodeEncoding.Unicode.GetBytes(text));
                    else
                        client.Client.Send(UnicodeEncoding.Unicode.GetBytes("[c=0;channel=default]" + text));
                }
            }
            catch (Exception)
            {
                Environment.Exit(0);
            }
        }

        static void dispMsg(IAsyncResult result)
        {
            try
            {
                int read = ((TcpClient)result.AsyncState).Client.EndReceive(result);
                Console.WriteLine(UnicodeEncoding.Unicode.GetString(buffer, 0, read));

                ((TcpClient)result.AsyncState).Client.BeginReceive(buffer, 0, buffer_size,
                    SocketFlags.None,
                    new AsyncCallback(dispMsg),
                    ((TcpClient)result.AsyncState));
            }
            catch (Exception)
            {
                Environment.Exit(0);
            }
        }
    }
}
